from loguru import logger

from app.common.quant_utils import get_real_quotes
from app.common.time_utils import Datetime
from app.macro import REVERSE_REPO_LIMIT


def get_security():
    today = Datetime.today()
    if today.weekday() == 4:
        return '131800.XSHE'  # r-003
    else:
        return '131810.XSHE'  # r-001


def get_quote(code):
    df_quotes = get_real_quotes('sz' + code)
    if df_quotes is None:
        logger.info(f'get {code} quote failed')
        return
    return df_quotes.iloc[0].to_dict()


def get_price(quote):
    for field in ['b5_p', 'b4_p', 'b3_p', 'b2_p', 'b1_p']:
        if quote[field] >= 0:
            return quote[field]
    logger.info(f'quote b1~5 price < 0')


def get_price_by_code(code):
    quote = get_quote(code)
    if not quote:
        return
    return get_price(quote)


def make_quantity_list(cash):
    quantity_list = []
    quantity = int(cash // 1000 * 10)
    while quantity > REVERSE_REPO_LIMIT:
        quantity_list.append(REVERSE_REPO_LIMIT)
        quantity -= REVERSE_REPO_LIMIT
    if quantity > 0:
        quantity_list.append(quantity)
    return quantity_list
